﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ecreate.Popedom;
namespace Ecreate.Web.popedom.menuManage
{
    public partial class menucrud : System.Web.UI.Page
    {
        Ecreate.Popedom.Service.IMenuService menuService = new Ecreate.Popedom.Service.SpringImpl.MenuServiceImpl();
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!this.IsPostBack)
            {
                IList<tMenu> list = menuService.getSearchAll();
                this.ddl_parentmenu.DataValueField = "Id";
                this.ddl_parentmenu.DataTextField = "menuCaption";
                this.ddl_parentmenu.DataSource = list;
                this.ddl_parentmenu.DataBind();


                this.hid_sendurl.Value = this.Request.UrlReferrer.AbsoluteUri;
                string action = this.Request.QueryString["action"];
                string id = this.Request.QueryString["id"];
                this.hid_action.Value = action;
                this.hid_id.Value = id;
                if (action == ActionType.delete.ToString())
                {
                    Ecreate.Popedom.tMenu menu = new Ecreate.Popedom.tMenu();
                    menu.Id = id;
                    menuService.DeleteMenu(menu);
                    this.Response.Redirect(this.hid_sendurl.Value);
                }
                if (action == ActionType.edit.ToString())
                {
                    Ecreate.Popedom.tMenu menu = menuService.getById(id);
                    this.txt_name.Text = menu.Menucaption;
                    this.txt_url.Text = menu.Menuurl;
                    if (menu.Menu == null)
                    {
                        this.ddl_parentmenu.SelectedValue = "";
                    }
                    else
                    {
                        this.ddl_parentmenu.SelectedValue = menu.Menu.Id;
                    }
                }

            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.hid_action.Value == ActionType.add.ToString())
            {
                Ecreate.Popedom.tMenu menu = new Ecreate.Popedom.tMenu();
                menu.Id = this.hid_id.Value;
                menu.Menucaption = this.txt_name.Text;
                menu.Menuurl = this.txt_url.Text;
                string value = this.ddl_parentmenu.SelectedValue;
                if (value == "")
                {
                    menu.Menu = null;
                }
                else
                {
                    menu.Menu = menuService.getById(value);
                }
                menuService.InsertMenu(menu);
            }
            if (this.hid_action.Value == ActionType.edit.ToString())
            {
                Ecreate.Popedom.tMenu menu = new Ecreate.Popedom.tMenu();
                menu.Id = this.hid_id.Value;
                menu.Menucaption = this.txt_name.Text;
                menu.Menuurl = this.txt_url.Text;
                string value = this.ddl_parentmenu.SelectedValue;
                if (value == "")
                {
                    menu.Menu = null;
                }
                else
                {
                    menu.Menu = menuService.getById(value);
                    if (menu.Menu.Id.Equals(menu.Id))
                    {
                        HtmlHelper.ShowAlertMessage(this, "顶级菜单不能选择自身!");
                        return;

                    }
                }
                menuService.UpdateMenu(menu);
            }
            this.Response.Redirect(this.hid_sendurl.Value);
        }
    }
}
